package com.tcs.teg.web.accounts;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.util.WebUtils;

import com.tcs.teg.domain.accounts.Language;
import com.tcs.teg.domain.accounts.MyPreferences;

//adding a comment
@Controller
public class MyPreferenceController {
	
	private static final Log logger = LogFactory.getLog(MyPreferenceController.class);
		
	@ModelAttribute("languages")
	public List<Language> getLanguages(){
		List<Language> avlblLanguageList = new ArrayList<Language>();
		avlblLanguageList.add(new Language("en_us", "English"));
		avlblLanguageList.add(new Language("hi_hi", "Hindi"));		
		return avlblLanguageList;
	}
	
	@RequestMapping("/myPreferences")
	public String showMyPreferences(Model model, RequestContext rc, HttpServletRequest request){
		logger.debug("Retrieving the user locale from session "+WebUtils.getSessionAttribute(request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME));		
		Locale locale = (Locale)WebUtils.getSessionAttribute(request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);		
		MyPreferences myPreference = new MyPreferences();
		if(locale !=null){
			logger.debug("locale value is "+locale.getLanguage());
			myPreference.setPreferredLanguage(locale.getLanguage());
		}	
		model.addAttribute(myPreference);
		return "myPreferences";
	}
	
	@RequestMapping(value="/myPreferences", method = RequestMethod.POST )
	public String saveMyPreferencesInSession(MyPreferences myPref,HttpServletRequest request ){
		logger.debug(WebUtils.getSessionAttribute(request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME));
		//get Language from MyPreferences
		String langCode = myPref.getPreferredLanguage();
		Locale locale = new Locale(langCode);
		WebUtils.setSessionAttribute(request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);		
		return "redirect:myPreferences";
	}
	
/*	@RequestMapping("/myPreferences")
	public String showMyPreferences(Model model, HttpServletRequest rc, 
								  @CookieValue(value="preferred_language", required=false) String key){
		
		logger.debug("Retrieving the user locale from cookie "+key);
		String prefLang = key;		
		if(prefLang ==  null){
			prefLang = rc.getLocale().getLanguage();
		}		
		MyPreferences myPreference = new MyPreferences();
		myPreference.setPreferredLanguage(prefLang);
		model.addAttribute(myPreference);
		return "myPreferences";
	}
	
	@RequestMapping(value="/myPreferences", method = RequestMethod.POST )
	public String saveMyPreferences(MyPreferences myPref, HttpServletResponse response ){		
		//get Language from MyPreferences
		String langCode = myPref.getPreferredLanguage();		
		//create cookie and add it to response
		Cookie  cookie = new Cookie("preferred_language",langCode);
		//save the user preferences
		response.addCookie(cookie);
		return "redirect:myPreferences";
	}*/
	
	

}
